home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8730 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  83 lines

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: polymorphism and style
  5. Date: 26 Feb 1996 20:46:45 +1100
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4grvi5$fs@werple.net.au>
  8. References: <4gr4ur$n13@alpha.pcix.com>
  9. NNTP-Posting-Host: werple.mira.net.au
  10.  
  11. ed lizewski <elizewsk@capecod.net> writes:
  12.  
  13. >  Question on polymorphism and style.
  14. >Want to call a function in a most derived class off a pointer to the base
  15. >class. The fuction does not exist in the base class.
  16. >I should:
  17. >1)downcast ?
  18.  
  19. This is one option. If your compiler has Run Time Type Information, one 
  20. of the more recent additions to C++, you can safely use 'dynamic_cast'.
  21. Example:
  22.     void f(Base *pb)
  23.     {
  24.         if(Derived *pd = dynamic_cast<Derived*>(pb))
  25.         { 
  26.         //Use 'pd'
  27.         }
  28.         else
  29.         { 
  30.         //'pb' is not a Derived*
  31.         }
  32.     }
  33. Without RTTI, you can use a traditional cast, but you have to know that 
  34. it is a Derived*.
  35.  
  36. >2)add the function to the base class and add virtual stubs to every 
  37. >  class that does not use it ? 
  38.  
  39. You shouldn't need to touch derived classes that have no interest in it.
  40.  
  41. >3)add it to the base class but define it like 
  42. >     foo( ..whatever..) {error_msg("virtual function not defined");}
  43. >  This way only the class that uses it would have to define it.
  44.  
  45. Normally, you would only put a function in the base class if it makes
  46. sense for it, i.e., ask yourself if this is a function you could
  47. reasonably expect any derived class to implement sensibly. If the answer
  48. is no, it probably shouldn't be in the base class. Also, if you plan to
  49. use it in only one derived class, you'll have to know that a pointer is of
  50. that class when you call the function to avoid the error message, and if
  51. you know what class it is you don't need the function in the base class;
  52. you can just cast the pointer (although the base class function may be
  53. useful in debugging). 
  54.  
  55. In the absence of RTTI, you could make an exception to simulate
  56. 'dynamic_cast'. Example:
  57.     class Circle;
  58.     class Square;
  59.  
  60.     class Shape
  61.     {
  62.         //...
  63.         virtual Circle *asCircle() { return 0; }
  64.         virtual Square *asSquare() { return 0; }
  65.     };
  66.  
  67.     class Circle : public Shape
  68.     {
  69.         //...
  70.         Circle *asCircle() { return this; }
  71.     };
  72. You can guess what class Square looks like.
  73. Sometimes you have no idea what class a Base* really is and you need to 
  74. know somehow. The above code is distasteful, but that's why RTTI was 
  75. introduced. 
  76. This technique is preferable to implementing the specific function in 
  77. the base class because there might be several functions you want to call 
  78. in the derived class; once you have a pointer to the derived class you 
  79. can call all its functions.
  80.  
  81. David White
  82. davidw@werple.mira.net.au
  83.